﻿using AutoMapper;
using B.S.BasicData.Api.Read.Application.Command.User;
using B.S.BasicData.Api.Read.DTOS;
using B.S.BasicData.Domain.Entity;
using B.S.BasicData.ErrorCode;
using B.S.BasicData.Infastructure.Interface;
using MediatR;

namespace B.S.BasicData.Api.Read.Application.Handler.User
{
    public class GetRoleListHandler : IRequestHandler<GetRoleListCommand, ApiResult<List<RoleDto>>>
    {
        private readonly IBaseRepository<RoleModel> _roleRepository;
        private readonly IMapper _mapper;

        public GetRoleListHandler(IBaseRepository<RoleModel> roleRepository, IMapper mapper)
        {
            _roleRepository = roleRepository;
            _mapper = mapper;
        }

        /// <summary>
        /// 获取角色列表数据
        /// </summary>
        /// <param name="request"></param>
        /// <param name="cancellationToken"></param>
        /// <returns></returns>
        /// <exception cref="NotImplementedException"></exception>
        public Task<ApiResult<List<RoleDto>>> Handle(GetRoleListCommand request, CancellationToken cancellationToken)
        {
            ApiResult<List<RoleDto>>result = new ApiResult<List<RoleDto>>();

            var role = _roleRepository.GetAll().Where(p => p.IsDelete == false);
            if (role != null)
            {
                result.Code = StatusEnum.OK;
                result.Data= _mapper.Map<List<RoleDto>>(role);
                result.Message = "获取角色数据成功";
            }
            return Task.FromResult(result); 
        }
    }
}
